package com.singerw.controller;

import com.alibaba.fastjson.JSONObject;
import com.singerw.annotations.OperLog;
import com.singerw.annotations.ResponseResult;
import com.singerw.annotations.UserName;
import com.singerw.pojo.TwTeam;
import com.singerw.service.TwTeamService;
import com.singerw.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @version V1.0
 * @Package com.singerw.controller
 * @auhter 张欣
 * @date 2021/1/19-4:07 PM
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TwTeamService twTeamService;

    /**
     * 如果请求的用户为「admin」将返回所有数据
     * @param username
     * @param isManage 如果参数为 0，则返回当前用户拥有权限的组织，反之返回此用户加入的组织。
     * @return
     */
    @GetMapping("/findByUserName/{isManage}")
    public String findByUserName(@UserName String username,
                                 @PathVariable("isManage") int isManage) {
        List<TwTeam> resultData;
        if(username.equals("admin")){
            resultData =  twTeamService.TeamList();
        }else{
            if (isManage == 0) {
                resultData = twTeamService.findByUserNameAndIsManage(username);
            }else{
                resultData = twTeamService.findByUserName(username);
            }
        }

        if(resultData.size() > 0) return ResultUtils.success(resultData);

        return ResultUtils.fail("未加入部门");
    }

    @GetMapping("/findByTeamName/{teamName}")
    public List<TwTeam> findByTeamName(@PathVariable("teamName") String teamName) throws UnsupportedEncodingException {
        teamName = URLDecoder.decode(teamName, "utf-8");
        return twTeamService.findByTeamName(teamName);
    }
    @GetMapping("/findByTeamRole/{teamName}/{teamRole}")
    public List<TwTeam> findByTeamRole(@PathVariable("teamName") String teamName,@PathVariable("teamRole") String teamRole) throws UnsupportedEncodingException {
        teamName = URLDecoder.decode(teamName, "utf-8");
        return twTeamService.findByTeamName(teamName);
    }
    @PostMapping("/add/userName")
    @ResponseResult
    public void addUserName(@RequestBody TwTeam twTeam) {
        twTeam.setTeamRole("admin");
        twTeamService.save(twTeam);
    }

    @PostMapping("/add/team")
    @ResponseResult
    @OperLog(operModul = "部门模块",operType = "添加成员/添加部门",operDesc = "后台添加部门或成员加入部门")
    public String addTeam(@RequestBody TwTeam twTeam) {
        twTeamService.save(twTeam);
        return "success";
    }

    @PostMapping("/remove/userName")
    @ResponseResult
    @OperLog(operModul = "部门模块",operType = "退出部门",operDesc = "成员退出部门")
    public void removeUserName(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        String teamName = jsonObject.getString("teamName");
        twTeamService.removeByUserNameFromTeam(userName, teamName);
    }

    @PostMapping("/remove")
    @ResponseResult
    @OperLog(operModul = "部门模块",operType = "删除部门",operDesc = "后台删除部门")
    public void removeTeam(@RequestBody JSONObject jsonObject) {
        String teamName = jsonObject.getString("teamName");
        twTeamService.removeByTeamName(teamName);
    }

    @GetMapping("/list")
    @ResponseResult
    public List<TwTeam> list(){
        return twTeamService.TeamList();
    }

}
